package com.ding.unsafe;

import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author ding
 * @date 2021/7/17 20:45
 *
 * ConcurrentModificationException
 * 1. Set<Object> set = Collections.synchronizedSet(new HashSet<>());
 */



public class SetTest {

    public static void main(String[] args) {

//        HashSet<String> set = new HashSet<>();
//        Set<Object> set = Collections.synchronizedSet(new HashSet<>());
        CopyOnWriteArraySet<String> set = new CopyOnWriteArraySet<>();
        for (int i = 0; i < 30; i++) {
            new Thread(
                    () -> {
                        set.add(UUID.randomUUID().toString().substring(0, 5));
                        System.out.println(set);
                    }
            ).start();
        }
    }
}
